//定义一个数值和字符串的联合类型
interface User {
    message: string | number
}
//定义一个类型断言
interface Fish {
    name: string;
    swim(): void;
}
//定义一个 void 实现函数的无返回值
function showMsg(): void {
    console.log('显示消息')
}
//定义一个 never 的函数
function fn(): never {
    throw new Error();
    console.log('Hello World');
}
//分别定义 es5 和 ts 的函数定义
//es5
function run(): string {
    return 'run';
}
var fun2 = function (): number {
    return 123;
}
//ts
function add(n1: number, n2: number): number {
    return n1 + n2;
};
console.log(add(1, 2));
var add1 = function (n1: number, n2: number): number {
    return n1 + n2;
};
console.log(add1(1, 2))
//定义函数时， 分别实现参数类型、剩余参数、默认参数
//可选参数
function say(x: number, y?: number): void {
    if (y) {
        console.log(x + y)
    } else {
        console.log(x)
    }
}
say(1);
say(2, 5)
//剩余参数
function push(array: any[], ...items: any[]) {
    items.forEach(item => {
        array.push(item)
    })
}
let a: any[] = []
push(a, 1, 2, 3, 4)

//默认参数
function buildName(firstName?: string, lastName: string = 'Cat') {
    return firstName + ' ' + lastName;
}
let tomcat = buildName('Tom', 'Cat');
let tom = buildName('Tom');

function buildNameOther(firstName: string, lastName: string = 'Cat') {
    return firstName + ' ' + lastName;
}

//实现一个函数重载
function reverse(x: number | string): number | string | void {
    if (typeof x === 'number') {
        return Number(x.toString().split('').reverse().join(''));
    } else if (typeof x === 'string') {
        return x.split('').reverse().join('');
    }
}

//使用 es5 的方式定义一个类和es6中定义类
//es5
function Person1(name) {
    this.name = name;
}
Person1.prototype.sayHello = function () {
    return 'Hi,I am' + this.lastName
}
//es6
class Person {
    [x: string]: any;
    constructor(name: any) {
        this.name = name;
    }
    sayHello() {
        return 'Hi, I am ' + this.name;
    }
}
typeof Person;
//在 ts 中定义一个类并实现继承
class min {
    name: String;
    age: Number;
    con: String;
    constructor(name: String, age: Number) {
        this.name = name;
        this.age = age;
    }
    getName(): String {
        return this.name
    }
    getAge(): Number {
        return this.age
    }
    setAge(age): void {
        this.age = age
    }
    setName(name): void {
        this.name = name
    }
}
class kowd extends min {
    con: String;
    constructor(name: String, age: Number, con: String) {
        super(name, age)
        this.con = con;
        console.log(con);
    }
}
let mint = new kowd('小明', 22, '我是新来的');
console.log(mint.getAge())
console.log(mint.getName())
mint.setAge(50)
console.log(mint.getAge())
mint.setName('小黄')
console.log(mint.getName())